<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		$this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
	}

	public function index()
	{
		$this->load->view('home');
	}

    public function register() {

        //Get menu
        $data['template']['menu'] = createMenu();
        if($this->is_login())
            $data['template']['user'] = $this->session->userdata('username');
        
        $this->load->view('view_register', $data);
    }
//====================================================================================   
//==============================REGISTER FUNCTION========================================
//====================================================================================

    public function get_register()
    {        
        // $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]');
        // $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|matches[repass]');
        // $this->form_validation->set_rules('repass', 'Password Confirmation','trim|required');
        // $this->form_validation->set_rules('email', 'Email', 'trim|required');

        $email = $this->input->post('email');                    
        $user = $this->input->post('username');
        $pass = md5($this->input->post('password'));
        $repass = md5($this->input->post('repass'));
        $fullname = $this->input->post('fullname');
        $gender = $this->input->post('gender');
        $dob = $this->input->post('dob');
        $phone = $this->input->post('phone');
        $job = $this->input->post('job');
        $care = $this->input->post('interestedin');

        $this->load->model('model_user');
        if($this->model_user->isset_user($user)) 
        {
            echo 'Username đã tồn tại';
        }
        else {
            $q = $this->model_user->add_user($user, $pass, $fullname, $gender, 
                $dob, $email, $phone, '', $job,$care );

            if ($q != -1) {
                $catchSession = array(
                    'username' => $this->input->post('username'),
                    'password' => md5($this->input->post('password')),
                    'is_logged_in' => true,
                );
                $this->session->set_userdata($catchSession);

                // redirect(base_url().'user');
                echo 'Đăng ký thành công';

            } 
            else {
                //$this->index();
                // $this->load->view('view_register');
                echo 'Đăng ký thất bại';
            } 
        }            
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */